package com.jy.rhin.support.interceptor.convertor;

import com.jy.rhin.application.enums.*;
import com.jy.rhin.facade.dto.SaveExternalSystemAuditInfoEvent;
import com.jy.rhin.sdk.dto.audit.AuditSourceDto;
import com.jy.rhin.sdk.dto.audit.AuditWrapperDto;
import com.jy.rhin.sdk.dto.audit.CodedValueTypeDto;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @Description: 外部系统互相调用的审计日志
 */
@Service("AuditInfoConverter4ExternalSystem")
public class AuditInfoConverter4ExternalSystem extends BaseAuditInfoConverter<SaveExternalSystemAuditInfoEvent, SaveExternalSystemAuditInfoEvent> {

    public AuditInfoConverter4ExternalSystem() {
        super(EventActionCodeEnum.READ,
                ServiceCodeEnum.AUDIT_TRAIL,
                EventTypeCodeEnum.RECORD_AUDIT_EVENT,
                true,
                ParticipantObjectIDTypeCodeEnum.SEARCH_CRITERIA,
                ParticipantObjectTypeCodeRoleEnum.QUERY,
                ParticipantObjectTypeCodeEnum.SYSTEM_OBJECT,
                ParticipantObjectDataLifeCycleEnum.ACCESS_USE);
    }

    @Override
    public AuditWrapperDto convertReq(SaveExternalSystemAuditInfoEvent req) {
        AuditWrapperDto dto = super.convertReq(req);

        dto.setSourceList(buildAuditSource(req));
        dto.getEventInfo().setShiJianBs (buildEventId(req));
        dto.getEventInfo().setFaQiSj(req.getDiaoYongSJ());
        return dto;
    }



    @Override
    public AuditWrapperDto convertResp(SaveExternalSystemAuditInfoEvent resp) {
        AuditWrapperDto dto = super.convertResp(resp);

        dto.setSourceList(buildAuditSource(resp));
        dto.getEventInfo().setShiJianBs (buildEventId(resp));
        dto.getEventInfo().setFaQiSj(resp.getDiaoYongSJ());
        return dto;
    }

    /**
     * 事件标识使用接口标识
     * @param req
     * @return
     */
    private CodedValueTypeDto buildEventId(SaveExternalSystemAuditInfoEvent req) {
        CodedValueTypeDto eventId = new CodedValueTypeDto();
        eventId.setCodeSystem(CodeSystemEnum.CV_EVENT_ID_CODE.getCode());
        eventId.setCodeSystemName(CodeSystemEnum.CV_EVENT_ID_CODE.getName());
        eventId.setCode(req.getJieKouBSF());
        eventId.setDisplayName(req.getJieKouMC());
        return eventId;
    }

    /**
     * 审计源（审计源为事件目标）
     */
    @Override
    List<AuditSourceDto> buildAuditSource() {
        return null;
    }

    /**
     * 使用请求的ip和标识符作为源信息
     * @param req
     * @return
     */
    List<AuditSourceDto> buildAuditSource(SaveExternalSystemAuditInfoEvent req) {
        AuditSourceDto auditSource = new AuditSourceDto();
        auditSource.setBiaoShi(req.getJieKouBSF());
        auditSource.setWangZhi(req.getJieKouDZ());
        CodedValueTypeDto auditSourceTypeCode = new CodedValueTypeDto();
        auditSourceTypeCode.setCodeSystem(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getCode());
        auditSourceTypeCode.setCodeSystemName(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getName());
        auditSourceTypeCode.setCode(AuditSourceTypeCodeEnum.WEB_SERVER_PROCESS.getCode());
        auditSourceTypeCode.setDisplayName(req.getJieKouMC());
        auditSource.setLeiXing(auditSourceTypeCode);
        return Collections.singletonList(auditSource);
    }

    @Override
    AuditParticipantUserDesc buildSourceUser(SaveExternalSystemAuditInfoEvent request, SaveExternalSystemAuditInfoEvent response) {
       if(request!=null){
           return new AuditParticipantUserDesc(request.getJieShouFDM(), request.getJieShouFMC());
       }else{
           return new AuditParticipantUserDesc(response.getJieShouFDM(), response.getJieShouFMC());
       }
    }

    @Override
    AuditParticipantUserDesc buildOriginatorUser(SaveExternalSystemAuditInfoEvent request, SaveExternalSystemAuditInfoEvent response) {
        if(request!=null){
            return new AuditParticipantUserDesc(request.getDiaoYongFDM(), request.getDiaoYongFMC());
        }else{
            return new AuditParticipantUserDesc(response.getDiaoYongFDM(), response.getDiaoYongFMC());
        }
    }

    @Override
    AuditParticipantUserDesc buildTargetUser(SaveExternalSystemAuditInfoEvent request, SaveExternalSystemAuditInfoEvent response) {
        if(request!=null){
            return new AuditParticipantUserDesc(request.getJieKouBSF(), request.getJieKouMC());
        }else{
            return new AuditParticipantUserDesc(response.getJieKouBSF(), response.getJieKouMC());
        }
    }

    @Override
    String getObjectId(SaveExternalSystemAuditInfoEvent request, SaveExternalSystemAuditInfoEvent response) {
        if (Objects.nonNull(request)) {
            return request.getJieKouBSF();
        }
        return response.getJieKouBSF();
    }

	@Override
	public boolean isMatch(String method, Object data) {
		return "externalSystem".equals(method);
	}

}
